package org.artifact.core.plugin.eventbus;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class GameEventBus {
	/** 监听器 */
	private Map<String, GameEventListener> listeners = new HashMap<>();
	/** 处理器 */
	private Map<Integer, GameEventHandler> handlers = new HashMap<>();

	/** 注册监听器 */
	public void registerListener(GameEventListener listener) {
		listeners.put(listener.getClass().getName(), listener);
	}
	
	/** 注册处理器 */
	public void registerHandler(GameEventHandler handler) {
		handlers.put(handler.getEventType(), handler);
	}

	/** 取消监听器 */
	public void unregisterListener(GameEventListener listener) {
		listeners.remove(listener.getClass().getName());
	}
	
	/** 取消处理器 */
	public void unregisterHandler(GameEventHandler handler) {
		handlers.remove(handler.getEventType());
	}
	
	public void push(GameEvent event) {
		for (Entry<String, GameEventListener> e : listeners.entrySet()) {
			e.getValue().handle(event);
		}
	}
	
	public void push(Class<GameEventListener> clazz,GameEvent event) {
		GameEventListener listener = listeners.get(clazz.getName());
		if (listener!=null) {
			listener.handle(event);
		}
	}
	
	public void asynPush(GameEvent event) {
		
	}
	
	public void asynPush(GameEventListener listener,GameEvent event) {
		
	}
}
